<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
	Sign Up
</asp:Content>

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
        <form  id="regForm" action="/Register/Go" method="post" enctype="multipart/form-data">
    <table cellpadding="15" cellspacing="15">
        <tr>
            <td>
                <h2>Sign Up</h2><br />
                or <a href="/login">Login</a>   
            </td>
        </tr>
        <tr>
            <td>
                Enter your email address
                
            </td>
            <td>
                <input type="text" class="input" name="email" onblur="EmailCheck(); return false" class="registration" onblur="" />
            </td>
            <td>    
                <span id="confirmE"></span>
            </td>
        </tr> 
        <tr>
            <td>
                First Name<br />
                
                
            </td>
            <td width="200px">
                <input type="text" class="input"  name="firstname" class="registration" />
            </td>
                      
        </tr>
        
         <tr>
            <td>
                Last Name<br />
                
                
            </td>
            <td width="200px">
                <input type="text" class="input"  name="lastname" class="registration" />
            </td>
                      
        </tr>
        
        <tr>
            <td>
                Choose a password<br />
                <span class="hint">Must be at least 5 characters.</span><br />
                
            </td>
            <td>
                <input type="password" class="input"  name="password"  class="registration" />
            </td>
        </tr>
        <tr>
            <td>
                Password again
                
                
            </td>
            <td>
                <input type="password" class="input"  name="password2" class="registration" />
            </td>
        </tr>
                
        <tr>
            <td>
                <input type="hidden" name="type" value="player" />
                <input type="submit" class="button"  value="Sign Up" />
            </td>
        </tr> 
           
    </table>

</form>

<div id="error"></div>
<script type="text/javascript">
 
 
 
 var goodE = false;
 var options = { dataType:'json',success:RegCallBack,beforeSubmit:Validate }; 
    $(document).ready(function() {

        $("input").focus(function () {$(this).css('border-color','#eecc00');$("#error").html("")});
        $("input").blur(function () {$(this).css('border-color','#999');});
        $('#regForm').ajaxForm(options);
        
    }); // end of document ready

    function Validate(){
        var filter = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
        if (document.getElementById("regForm").firstname.value == "" ||
        document.getElementById("regForm").lastname.value == "" ||
        document.getElementById("regForm").password.value == "" ||
        document.getElementById("regForm").email.value == "")
        {
             $("#error").html("Please complete all fields.");
            return false;
        }
        
        if(!goodE){
            $("#error").html("The email you entered is not valid. Please try another.");
            return false;
        }
        if (!filter.test(document.getElementById("regForm").email.value)) {
        
            $("#error").html("Please enter a valid email address.");
            return false;
        }
        if (document.getElementById("regForm").password.value.length < 5)
        {
        
            $("#error").html("Password too short.");
            return false;
        }
        if (document.getElementById("regForm").password.value != document.getElementById("regForm").password2.value) {
            $("#error").html("Passwords do not match.");
            return false;

        }
       
            
           $("#error").css("color","green");
           $("#error").html("Registering " + document.getElementById("regForm").firstname.value + "...");
           return true;       
       
       
    }
    
   
    function EmailCheck(){
        goodE = false;
        var email = document.getElementById("regForm").email.value;
        if (email == "") return false;
        //$("#confirm").html("<img src='../../Assets/Images/loading.gif' width='25' height='25' alt='loading'/>");
        $.getJSON('/register/emailcheck/?email=' + email,
            function(data)
            {
                if(data != null)
                {
                    if(data.Successful.toString() == "true") 
                    {
                        $("#confirmE").css("color","green");
                        $("#confirmE").html("Available");
                        $("#error").html("");
                        goodE = true;
                        //$('#regForm').ajaxSubmit(options);
                        return;
                    }
                    $("#confirmE").css("color","red");
                    $("#confirmE").html("Unavailable");
                    return false;
                    
                }
            });
          return false;
        
    }
    
    function RegCallBack(data)
    {
        
        if(data.Successful)
        {
            $("#error").css("color","green");
            $("#error").html("Success!");   
//             var ret = window.location.search.substr(window.location.search.indexOf("=") + 1,window.location.search.length)
//             if(ret == "" || ret == "user") ret ="/User/Setup/" + data.ReturnVal;
            window.location = "/register/package";
        }
        else{
            $("#error").css("color", "red");
            $("#error").html("Oops, something went wrong. Please try again.<br />" + data.Exception);
        }
        
        
    
    }
    
    
 </script>

</asp:Content>

